import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJDBCDelete {
    public static void main(String[] args) throws SQLException {

        //创建数据源
        DataSource dataSource = new MysqlDataSource();

        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/schoolmessage?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        //建立连接
        Connection connection = dataSource.getConnection();

        Scanner scanner = new Scanner(System.in);

        //输入数据
        System.out.println("输入你想要删除的ID:");
        int id = scanner.nextInt();

        //操作数据库
        String sql = "delete from student where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setInt(1,id);

        System.out.println("preparedStatement:" + preparedStatement);

        //执行
        int ret = preparedStatement.executeUpdate();
        System.out.println("有" + ret + "行记录受到了影响");

        //回收资源
        preparedStatement.close();
        connection.close();


    }
}
